package com.wangcw.minicat;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
 
public class DynamicClassLoader extends ClassLoader{
    

    @SuppressWarnings("deprecation")
    @Override
    protected Class<?> findClass(String classPath) throws ClassNotFoundException {
        if(classPath != null){
            byte[] clazz = loadClazz(classPath);
            return defineClass(clazz, 0, clazz.length); 
        }else{
            System.out.println("class is not found !");
            return null;
        }
    }
 
    public byte[] loadClazz(String classPath) { 
        try { 
            FileInputStream in = new FileInputStream(new File(classPath));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int b;
            while((b = in.read()) != -1){
                baos.write(b);
            }
            in.close();
            return baos.toByteArray();
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

}